module.exports = (sequelize, DataTypes) => {
  const electric_sale = sequelize.define(
    'electric_sale',
    {
      id: {
        type: DataTypes.INTEGER(11),
        primaryKey: true,
        autoIncrement: true
      },
      purchaseTime: {
        type: DataTypes.STRING,
        allowNull: false,
        field: "purchase-time"
      },
      customerId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        field: "customer-id"
      },
      brandId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        field: "brand-id"
      },
      price: {
        type: DataTypes.DECIMAL,
        allowNull: false
      },
      remark: {
        type: DataTypes.STRING,
      },
      operateId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        field: "operate-id"
      },
      billImageUrl: {
        type: DataTypes.STRING,
        field: "bill-image-url"
      },
      paymentId: {
        type: DataTypes.INTEGER,
        field: "payment-id",
        allowNull: false
      },
      paymentBank: {
        type: DataTypes.STRING,
        field: "payment-bank"
      },
      isExchange: {//1 true; 0 false
        type: DataTypes.INTEGER,
        field: "is-exchange"
      },
      exchangeGoods: {
        type: DataTypes.STRING,
        field: 'exchange-goods'
      },
      isDivide: {//1 true; 0 false
        type: DataTypes.INTEGER,
        field: 'is-divide'
      },
      dividePeriods: {
        type: DataTypes.INTEGER,
        field: 'divide-periods'
      },
      nowDividePeriods: {
        type: DataTypes.INTEGER,
        field: 'now-divide-periods'
      },
      divideStartDatetime: {
        type: DataTypes.STRING,
        field: "divide-start-datetime"
      },
      divideEndDatetime: {
        type: DataTypes.STRING,
        field: "divide-end-datetime"
      },
      dividePrice: {
        type: DataTypes.DECIMAL,
        field: "divide-price"
      },
      statusId: {
        type: DataTypes.INTEGER,
        field: "status-id",
        allowNull: false
      }
    },
    {
      tableName: 'electric_sale',
      timestamps: false,//使用事务时，出现Unknown column 'created_at' in 'field list'，原因是sequelize默认会自动为其添加 createdAt 和updatedAt column值，添加timestamp处理
    },
  )

  electric_sale.associate = (models) => {//定义外键关联，与customers model的定义匹配。这里，foreignKey定义的是customers与electric_sale外键关联的字段，sourceKey才是定义electric_sale自己外键是哪个字段的。不定义sourceKey的话，系统默认与主键关联。。。坑爹，api上没看见hasMany方法的options里面有这个sourceKey的配置，我可能看了一个假api。。。
    electric_sale.hasMany(
      models.customers,
      {foreignKey: 'id',
      sourceKey: 'customer-id'}
    );
    electric_sale.hasMany(
      models.brands,
      {foreignKey: 'id',
      sourceKey: 'brand-id'}
    );
    electric_sale.hasMany(
      models.operators,
      {foreignKey: 'id',
      sourceKey: 'operate-id'}
    );
    electric_sale.hasMany(
      models.payment,
      {foreignKey: 'id',
      sourceKey: 'payment-id'}
    );
    electric_sale.hasMany(
      models.status,
      {foreignKey: 'id',
      sourceKey: 'status-id'}
    );
  }

  return electric_sale;
};